// license: creative commons cc0 1.0 (public domain)
// http://creativecommons.org/publicdomain/zero/1.0/

// freewiki 0.6, jan 2019 mn

function parse(s, p, l) {
    return s.substring(p, p + l);
    }

function right(s, l) {
    return s.substring(s.length-l, s.length);
    }

function left(s, l) {
    return s.substring(0, l);
    }

function back(s, l) {
    return s.substring(0, s.length-l);
    }
	
function extracthead(s){
	var h = s.substring(1,s.length-2);
	return h
}

cc = wiki.innerHTML; // storing the contents within the span tag
c = "";
plen = 0;
pre = 0;
it = 0;
bo = 0;
link = 0;
linkcopy = "";
lit = 0;
litcopy = "";
intag = 0;
inwiki = 0;
wikicopy = "";
colour = 0;
head = 0;

var colour_tag = ["[c1]","[c2]","[c3]","[c4]","[c5]","[c6]","[c7]","[c8]","[c9]","[c10]","[c11]","[c12]","[c13]","[c14]","[c15]","[c16]"];
var c_list = ["(0, 0, 0)", "(0, 0, 170)", "(0, 170, 0)", "(0, 170, 170)","(70, 0, 0)", "(170, 0, 170)", "(170, 85, 0)", "(170, 170, 170)", "(85, 85, 85)", 
"(85, 85, 255)", "(85, 255, 85)", "(85, 255, 255)", "(255, 85, 85)", "(255, 85, 255)", "(255, 255, 85)", "(255, 255, 255)"];

for(count = 0; count < cc.length; count++){
    p = parse(cc, count, 1); plen += 1; // extracting each character from cc

    if (p == "<" && pre == 0) {
        intag = 1;
        }
    if (p == ">" && pre == 0 && intag == 1) {
        intag = 0;
        }

    if (link == 1) {
        linkcopy += p;
        }

    if (inwiki == 1) {
        wikicopy += p;
        }

    if (p == "\n") {
        if (plen < 5 && pre == 1) {
            c += "</pre>";
            pre = 0;
            }
        c += "<br>"; plen = 0 ;

    } else if (p == "*" && pre == 0 && link == 0 && intag == 0 && lit == 0) {
        if (bo == 0) {
            c += "<b>";
            bo = 1;
        } else {
            c += "</b>";
            bo = 0;
            }

    } else if (p == "/" && pre == 0 && link == 0 && intag == 0 && lit == 0 && inwiki == 0) {
        if (it == 0) {
            c += "<i>";
            it = 1;
        } else {
            c += "</i>";
            it = 0;
            }

    } else if (p == "<" && pre == 1) {
        c += "&lt;";

    } else if (p == ">" && pre == 1) {
        c += "&gt;";

    } else {
        c += p;
        }

    if (plen == 4) {
        if (right(c, 4) == "    ") {

            if (pre == 0) {
                c = back(c, 4) + "<pre>";
            } else {
                c = back(c, 4) + "";
                }
            pre = 1;

        } else {

            if (pre == 1) {
                c = back(c, 4) + "</pre>" + right(c, 4);                    
                }
            pre = 0;

            }
        }

    if (intag == 0 && pre == 0 && right(c, 5) == "[url]") {
        if (link == 0) {
            c = back(c, 5) + '<a href="';
            link = 1;
        } else {
            c = back(c, 5) + '">' + back(linkcopy, 5) + '</a>';
            link = 0;
            linkcopy = "";
            }
        }

    if (intag == 0 && pre == 0 && right(c, 5) == "[lit]") {
        if (lit == 0) {
            c = back(c, 5);
            lit = 1;
        } else {
            c = back(c, 5) + '<!-- -->';
            lit = 0;
            litcopy = "";
            }
        }

    if (intag == 0 && inwiki == 0 && pre == 0 && right(c, 2) == "[[") {
        if (inwiki == 0) {
            c = back(c, 2) + '<a href="';
            inwiki = 1;
            }
        }

    if (intag == 0 && inwiki == 1 && pre == 0 && right(c, 2) == "]]") {
        if (inwiki == 1) {
            c = back(c, 2) + '.html">' + back(wikicopy, 2) + '</a>';
            inwiki = 0;
            wikicopy = "";
            }
        }

    if (intag == 0 && pre == 0 && right(c, 5) == "[img]") {
        if (link == 0) {
            c = back(c, 5) + '<img src="';
            link = 1;
        } else {
            c = back(c, 5) + '">';
            link = 0;
            linkcopy = "";
            }
        }
	if (pre == 0 && link == 0 && intag == 0 && lit == 0) { // for heading 
		if(right(c, 4) == "[h1]" || right(c, 4) == "[h2]" || right(c, 4) == "[h3]"){
			h= right(c,4)
			newh = h.substring(1,3);
			if(head == 0 ){c = back(c,4)+ "<" + newh + ">" ; head =1;}
		    else{ c = back(c,4)+ "</" + newh + ">" ; head =0; }
		}
		
	}
// colour text 	
	if (pre == 0 && link == 0 && intag == 0 && lit == 0 && right(c, 5) == "[red]"){
	if (colour ==0){
		c= back(c,5) + "<font style=\"color:#FF0000\";>";
		colour = 1;
	}else{
		c = back(c,5) + "</font>" 
		colour = 0
	}
	}
	
	
 if (pre == 0 && link == 0 && intag == 0 && lit == 0) {// for colour
	for(var i=0 ; i< 16; i++){
		if(colour_tag[i]==right(c,5) || colour_tag[i]==right(c,4) ){
			var len = colour_tag[i].length;
			if(colour == 0){
			c = back(c,len) + "<font style=\"color:rgb"   +c_list[i]+ "\";>";
			//c = back(c,len) + "<p> start </p>";
			colour =1;
			}else{
				c = back(c,len) + "</font>";
				colour =0;
			}
			
		}
	}
 }

    if (intag == 0 && pre == 0 && right(c, 9) == "[cc0-1.0]") {
        c = back(c, 9) + "<b>[</b>license: <a title='WWW link' href='http://creativecommons.org/publicdomain/zero/1.0/'>cc0 1.0</a><b>]</b>";
        }

    var rf = "" ;
    rf = window.location.href.split('/')[window.location.href.split('/').length - 1].split('.html')[0] ;
    
    if (intag == 0 && pre == 0 && right(c, 16) == "[generate-title]") {
        if (link == 0) {
            c = back(c, 16) + rf ;
            }
        }
}

wiki.innerHTML=c;


